package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/*
 * 发布到注册中心
 * */
@Service
public class RecommendUserApiImpl implements RecommendUserApi {

    //注入MongoDB操作的模板对象
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserLikeApi userLikeApi;

    //根据用户的id查询到得分最高的用户
    @Override
    public RecommendUser findBytoUserId(Long toUserId) {

        //创建查询条件对象 根据当前用户id查找 分数倒序的第一个 即分数最高的
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    //根据用户id查找所有推荐好友,并按照得分降序排列
    @Override
    public List<RecommendUser> findAllByUid(Long userId, List<Long> balckIds, Integer pageNum, Integer pageSize) {
        //SpringBoot JPA分页 PageRequest.of代替过时的PageRequest方法
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Order.desc("score")));
        //创建查询条件
        Query query = Query.query(Criteria.where("toUserId").is(userId).and("userId").nin(balckIds)).with(pageRequest);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return recommendUsers;
    }

    //查询推荐好友的缘分值
    @Override
    public Double queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (null != recommendUser) {
            return recommendUser.getScore();
        }
        return null;
    }

    //根据id查看推荐
    @Override
    public RecommendUser queryScore(Long uid, String id) {

        Query query = Query.query(Criteria.where("toUserId").is(uid).and("userId").is(Convert.toLong(id)));
        return mongoTemplate.findOne(query, RecommendUser.class);


    }

    //查询探花列表，查询时需要排除不喜欢列表用户
    @Override
    public List<RecommendUser> queryCardList(Long userId, Integer count, List<Long> blackUserIds) {
        //设置分页以及排序，按照得分倒序排序
        PageRequest pageRequest = PageRequest.of(0, count, Sort.by(Sort.Order.desc("score")));
        //排除已喜欢或不喜欢的用户
        List<Long> userIds = new ArrayList<>();
        //查询喜欢列表
        userIds.addAll(this.userLikeApi.queryLikeList(userId));
        //查询不喜欢列表
        userIds.addAll(this.userLikeApi.queryNotLikeList(userId));
        //同时排除黑名单用户
        for (Long blackUserId : blackUserIds) {
            userIds.add(blackUserId);
        }
        //构造查询条件
        Criteria criteria = Criteria.where("toUserId").is(userId);
        if (CollUtil.isNotEmpty(userIds)) {
            //加入到查询条件中，排除这些用户
            criteria.andOperator(Criteria.where("userId").nin(userIds));
        }
        Query query = Query.query(criteria).with(pageRequest);
        List<RecommendUser> recommendUserList = this.mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }

}
